#include "gtimer.h"
#include "BSP/LED/led.h"

TIM_HandleTypeDef g_timx_handle;

void gtim_timx_int_init(int arr, int psc)
{
	GTIM_TIMX_INT_CLK_ENABLE();
	g_timx_handle.Instance = GTIM_TIMX_INT;
	g_timx_handle.Init.Prescaler = psc;
	g_timx_handle.Init.CounterMode = TIM_COUNTERMODE_UP;
	g_timx_handle.Init.Period = arr;
	HAL_TIM_Base_Init(&g_timx_handle);
	
	HAL_NVIC_SetPriority(GTIM_TIMX_INT_IRQn, 1, 3);
	HAL_NVIC_EnableIRQ(GTIM_TIMX_INT_IRQn);
	
	HAL_TIM_Base_Start_IT(&g_timx_handle);
}

void GTIM_TIMX_INT_IRQHandler(void)
{
	if (__HAL_TIM_GET_FLAG(&g_timx_handle, TIM_FLAG_UPDATE) != RESET) {
		LED1_TOGGLE();
		__HAL_TIM_CLEAR_IT(&g_timx_handle, TIM_IT_UPDATE);
	}
}